package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.util.Log;

import java.util.ArrayList;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdateHospitalStaticNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalTakeProgressViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by sinyo on 2017/7/25.
 */

public class InHospitalTakeProgressPresenter extends BasePresenter<InHospitalTakeProgressViewModel> {
    ArrayList<InHospitalPatientBeen> FitstTotal;
    //    private ArrayList<InHospitalPatientBeen> mTestBeen=new ArrayList<>();
    private ApiService mApiService;
    private MDialog waitDialog;
    public String DJJLID;
    @Override
    public void onCreatePresenter() {
        loadData(false);
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
    }

    @Override
    public void loadData(boolean ref) {
        FitstTotal = (ArrayList<InHospitalPatientBeen>) activity.getIntent().getExtras().getSerializable("total");
        DJJLID=activity.getIntent().getExtras().getString("DJJLID");
        Log.e("PXY", "DoorTakeProgressPresenter: "+FitstTotal.toString() );
        if(FitstTotal!=null ){
            viewModel.show(FitstTotal);
        }
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    public boolean confimYPSPBM(String YPSPBM){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null !=viewModel.remove && null != viewModel.remove.getYPSPBM()){
            if(viewModel.remove.getYPSPBM().equals(YPSPBM)){
                return true;
            }
        }
        return false;
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    public boolean confimYPJGBM(String YPJGBM){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null !=viewModel.remove && null != viewModel.remove.getYPJGBM()){
            if(viewModel.remove.getYPJGBM().equals(YPJGBM)){
                return true;
            }
        }
        return false;
    }
    //TODO:扫码获取的属性与当前集合中的要被移除的Bean进行对比
    public boolean confimKFWZMC(String KFWZMC){
//        Log.e(TAG,"viewModel.remove:"+viewModel.remove.toString());
        if(null !=viewModel.remove && null != viewModel.remove.getKFWZMC() && viewModel.remove.getKFWZMC().length()>=7){
            if(viewModel.remove.getKFWZMC().substring(0,7).equals(KFWZMC)){
                return true;
            }
        }
        return false;
    }

    /**
     *  判断是口服还是 针剂
     */
    public void queryKForZJ(){
        if(null != DJJLID && DJJLID.length()>1){
            if(DJJLID.substring(0,1).equalsIgnoreCase("z")){
                queyZJ("8",DJJLID.substring(1,DJJLID.length()));
            }else{
                queyKF("8",DJJLID);
            }
        }
    }

    /**
     * 口服和针剂的判断状态  是否允许进行更新
     * @param CXDJLX
     * @param DJJLID
     */
    public void queyKF(String CXDJLX, final String DJJLID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(QueryNode.getContent("PDAQ09",CXDJLX,DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        QueryBeen queryBeen = XmlUtils.getQueryBeen(xmlData.body.roleInfoResponse.retXml);
                        if( null != queryBeen){
//                            Log.e(TAG, "apply: "+queryBeen.toString() );
                            if(Contants.FIRST.equals(queryBeen.getDJDQZT())){
                                //TODO: 判断是口服还是针剂药房的
                                 updateKF(DJJLID,"1", PreferenceManager.getInstance().getLUserId());
                                return;
                            }
                            switch (queryBeen.getDJDQZT()){
                                case Contants.HOSPITAL_STATIC_TAKE:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已取,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已取,请重新确认");
                                    break;
                                case Contants.HOSPITAL_STATIC_CHECK:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已复核,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已复核,请重新确认");
                                    break;
                                case Contants.SEND:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已送出,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已送出,请重新确认");
                                    break;
                                case Contants.RECEIVER:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已接收,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已接收,请重新确认");
                                    break;
                                case Contants.ERROR:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为异常,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为异常,请重新确认");
                                    break;
                            }
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    /**
     * 口服和针剂的判断状态  是否允许进行更新
     * @param CXDJLX
     * @param DJJLID
     */
    public void queyZJ(String CXDJLX, final String DJJLID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(QueryNode.getContent("PDAQ09",CXDJLX,DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        QueryBeen queryBeen = XmlUtils.getQueryBeen(xmlData.body.roleInfoResponse.retXml);
                        if( null != queryBeen){
//                            Log.e(TAG, "apply: "+queryBeen.toString() );
                            if(Contants.FIRST.equals(queryBeen.getDJDQZT())){
                                //TODO: 判断是口服还是针剂药房的
                                updateZJ(DJJLID,"1", PreferenceManager.getInstance().getLUserId());
                                return;
                            }
                            switch (queryBeen.getDJDQZT()){
                                case Contants.HOSPITAL_STATIC_TAKE:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已取,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已取,请重新确认");
                                    break;
                                case Contants.HOSPITAL_STATIC_CHECK:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已复核,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已复核,请重新确认");
                                    break;
                                case Contants.SEND:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已送出,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已送出,请重新确认");
                                    break;
                                case Contants.RECEIVER:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已接收,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为已接收,请重新确认");
                                    break;
                                case Contants.ERROR:
//                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为异常,请重新确认");
                                    mOnSubmitResult.submitFail("此取药单当前状态为异常,请重新确认");
                                    break;
                            }
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    //TODO:更新药单  药单已完成  PDAU01
//   DJJLID	处方号	Varchar2(20)
//    DJDQZT	单据当前状态 	Number(1)
//    CZYHID	操作用户ID	Varchar2(20)
    public void updateKF(String DJJLID,String DJDQZT, String CZYHID) {
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(UpdateHospitalStaticNode.getTakeContent("PDAU03", DJJLID, DJDQZT, CZYHID,"1")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);

                        if ("-1".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitFail("服务器异常!");
                        } else if ("0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitSuccess();
                        }else {
                            mOnSubmitResult.submitFail("服务器异常");
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void updateZJ(String DJJLID,String DJDQZT, String CZYHID) {
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(UpdateHospitalStaticNode.getTakeContent("PDAU03", DJJLID, DJDQZT, CZYHID,"3")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        if ("-1".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitFail("服务器异常！");
                        } else if ("0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitSuccess();
                        }else {
                            mOnSubmitResult.submitFail("服务器异常");
                        }
                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
//                       waitDialog.dismiss();
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
//    //    KFWZMC	药品摆放位置
//    public void setPlace(final String KFWZMC) {
//        Disposable disposable = null;
//        boolean result;
//        disposable = mApiService.doXml(XmlUtils.prepare(UpdateBoxNode.getContent("PDAU02", DJJLID, KFWZMC)))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
//
//                    @Override
//                    protected void onStart() {
//                        if(null == waitDialog){
//                            waitDialog = CreateDialog.waitingDialog(activity);
//                        }
//                    }
//                    @Override
//                    public void onNext(RoleInfoResponseEnvelope xmlData) {
//                        CreateDialog.dismiss(activity);
////                               waitDialog.dismiss();
//                        if (null != mOnScanResult) {
//                            if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
//                                mOnScanResult.scanFail("设置缓存柜失败");
//                                return;
//                            }
//                            mOnScanResult.scanSuccess(KFWZMC);
//                        }
//                    }
//                    @Override
//                    public void onComplete() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        CreateDialog.dismiss(activity);
//                        if(null != e && null != e.getMessage()){
//                            Log.e("Throwable", e.getMessage());
//                            new MyToast(activity).showinfo(e.getMessage());
//                        }
//                    }
//                });
//        addDisposable(disposable);
//    }

    private OnScanResult mOnScanResult;

    public void registeOnScanResultListener(OnScanResult mOnScanResult) {
        this.mOnScanResult = mOnScanResult;
    }

    public void unRegisteOnScanResultListener() {
        this.mOnScanResult = null;
    }
    //扫码监听
    public interface OnScanResult {
        void scanSuccess(String KFWZMC);
        void scanFail(String errMsg);
    }

    //药单更新监听
    private OnSubmitResult mOnSubmitResult;

    public void registeOnSubmitResultListener(OnSubmitResult mOnScanResult) {
        this.mOnSubmitResult = mOnScanResult;
    }

    public void unRegisteOnSubmitResultListener() {
        this.mOnSubmitResult = null;
    }
    //药单更新监听
    public interface OnSubmitResult {
        void submitSuccess();
        void submitFail(String errMsg);
    }
}
